
MenuTitleR = 143
MenuTitleG = 96
MenuTitleB = 72
MenuTitleA = 135

MenuButtonR = 205
MenuButtonG = 192
MenuButtonB = 176
MenuButtonA = 100

MenuActiveButtonR = 248
MenuActiveButtonG = 248
MenuActiveButtonB = 255
MenuActiveButtonA = 135
































--[[
	create menus with AnimatedActionsGUI.createMenu(menuName, prevMenu, title, xpos, ypos, xscale, yscale, buttonSpacing, textScale, font)

	add buttons to each menu with AnimatedActionsGUI.addButton(parentMenu, text, funct, args, toggleable, textScale, font)
]]


AnimatedActionsGUI = {}
AnimatedActionsGUI.menu = {}
AnimatedActionsGUI.buttons = {}
AnimatedActionsGUI.navButtons = 	{
				["menuOpen"] = 
				{
					["keys"] = {},
					["menu"] = ""
				}, 
				["menuClose"] = {["keys"] = {}},
				["menuPrev"] = {["keys"] = {}},
				["navUp"] = {["keys"] = {}},
				["navDown"] = {["keys"] = {}},
				["select"] = {["keys"] = {}},
				["navBack"] = {["keys"] = {}}
			}
AnimatedActionsGUI.activeButton = 1
AnimatedActionsGUI.activeMenu = ""
AnimatedActionsGUI.statusText = {["text"] = "", ["time"] = 0}

AnimatedActionsGUI.titleColor = {["r"] = MenuTitleR, ["g"] = MenuTitleG, ["b"] = MenuTitleB, ["a"] = MenuTitleA}
AnimatedActionsGUI.buttonColor = {["r"] = MenuButtonR, ["g"] = MenuButtonG, ["b"] = MenuButtonB, ["a"] = MenuButtonA}
AnimatedActionsGUI.activeButtonColor = {["r"] = MenuActiveButtonR, ["g"] = MenuActiveButtonG, ["b"] = MenuActiveButtonB, ["a"] = MenuActiveButtonA}


function AnimatedActionsGUI.createMenu(menuName, prevMenu, title, xpos, ypos, xscale, yscale, buttonSpacing, textScale, font, saveButtonPos)
	if(AnimatedActionsGUI.menu[menuName] == nil) then
		AnimatedActionsGUI.menu[menuName] = {}
		lastOffset = ypos + yscale + buttonSpacing
	end
	
	AnimatedActionsGUI.menu[menuName] = {
		["prevMenu"] = prevMenu,
		["title"] = title,
		["xpos"] = xpos,
		["ypos"] = ypos,
		["xscale"] = xscale,
		["yscale"] = yscale,
		["spacing"] = buttonSpacing,
		["lastOffset"] = lastOffset,
		["numButtons"] = 0,
		["textScale"] = textScale,
		["lastButton"] = 1,
		["saveButtonPos"] = saveButtonPos,
		["font"] = font
	}
end

function AnimatedActionsGUI.addButton(parentMenu, text, funct, args, toggleable, textScale, font)
	--I tried to make this easy to follow (it probably doesn't help read it, but oh well), but basically,
	--
	if(AnimatedActionsGUI.buttons[parentMenu] == nil or AnimatedActionsGUI.buttons[parentMenu]["settings"] == nil) then
		AnimatedActionsGUI.buttons[parentMenu] = {["settings"] = {}}
	end

	local currButtonNum = #AnimatedActionsGUI.buttons[parentMenu].settings + 1
	AnimatedActionsGUI.menu[parentMenu].numButtons = currButtonNum
	--in other words, position the button x.spacing under the previous button

	if(toggleable) then
		toggledOn = false
	else
	    toggledOn = nil
	end

	AnimatedActionsGUI.buttons[parentMenu].settings[currButtonNum] = {
		["parentMenu"] = parentMenu,
		["text"] = text,
		["funct"] = funct,
		["args"] = args,
		["font"] = font,
		["xpos"] = AnimatedActionsGUI.menu[parentMenu].xpos,
		["ypos"] = AnimatedActionsGUI.menu[parentMenu].lastOffset,
		["xscale"] = AnimatedActionsGUI.menu[parentMenu].xscale,
		["yscale"] = AnimatedActionsGUI.menu[parentMenu].yscale,
		["textScale"] = textScale,
		["toggleable"] = toggleable,
		["toggledOn"] = toggledOn
	}

	AnimatedActionsGUI.menu[parentMenu]["lastOffset"] = (AnimatedActionsGUI.menu[parentMenu].lastOffset + AnimatedActionsGUI.menu[parentMenu].yscale + AnimatedActionsGUI.menu[parentMenu].spacing)
end

function AnimatedActionsGUI.setActiveMenu(menuName)
	AnimatedActionsGUI.activeMenu = menuName
	if(AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu] ~= nil) then
		if(AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].saveButtonPos or AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].saveButtonPos == nil) then
			AnimatedActionsGUI.activeButton = AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].lastButton
		else
		    AnimatedActionsGUI.activeButton = 1
		end
	end
end

function AnimatedActionsGUI.updateStatusText(text, time)
	AnimatedActionsGUI.statusText = {["text"] = text, ["time"] = time}
end

function AnimatedActionsGUI.isActiveButtonToggledOn()
	local activeButton = AnimatedActionsGUI.buttons[AnimatedActionsGUI.activeMenu].settings[AnimatedActionsGUI.activeButton]
	if(activeButton.toggleable) then
		return activeButton.toggledOn
	elseif(activeButton.toggleable == false) then
	    return nil
	end
end

function AnimatedActionsGUI.isButtonToggledOn(menu, index)
	local activeButton = AnimatedActionsGUI.buttons[menu].settings[index]
	if(activeButton.toggleable) then
		return activeButton.toggledOn
	elseif(activeButton.toggleable == false) then
	    return nil
	end
end

function AnimatedActionsGUI.toggleActiveButtonState()
	local activeButton = AnimatedActionsGUI.buttons[AnimatedActionsGUI.activeMenu].settings[AnimatedActionsGUI.activeButton]


	if(AnimatedActionsGUI.isActiveButtonToggledOn() and activeButton.toggleable) then
		activeButton.toggledOn = false
	elseif(AnimatedActionsGUI.isActiveButtonToggledOn() == false and activeButton.toggleable) then
		activeButton.toggledOn = true
	end
end

function AnimatedActionsGUI.toggleButtonState(menu, index)
	local activeButton = AnimatedActionsGUI.buttons[menu].settings[index]
	
	if(AnimatedActionsGUI.isButtonToggledOn(menu, index) and activeButton.toggleable) then
		activeButton.toggledOn = false
	elseif(AnimatedActionsGUI.isButtonToggledOn(menu, index) == false and activeButton.toggleable) then
		activeButton.toggledOn = true
	end
end

--todo
function AnimatedActionsGUI.removeButton(parentMenu, index)
	AnimatedActionsGUI.buttons[parentMenu].settings[index] = nil
	AnimatedActionsGUI.menu[parentMenu]["lastOffset"] = (AnimatedActionsGUI.menu[parentMenu].lastOffset - AnimatedActionsGUI.menu[parentMenu].yscale - AnimatedActionsGUI.menu[parentMenu].spacing)
end

--todo
function AnimatedActionsGUI.removeMenu(menuName)
	AnimatedActionsGUI.menu[menuName] = nil
end

function AnimatedActionsGUI.drawStatusText()
	--this is called every frame from AnimatedActionsGUI.tick()
	--this is pretty much a work around because using a while loop causes the game to freeze/crash
	--and using main:Wait(0) causes the text to flash instead of constant
	if(nextDrawTime == nil or prevText ~= AnimatedActionsGUI.statusText.text) then
		nextDrawTime = natives.GAMEPLAY.GET_GAME_TIMER() + AnimatedActionsGUI.statusText.time
	end

	if(nextDrawTime > natives.GAMEPLAY.GET_GAME_TIMER()) then
		prevText = AnimatedActionsGUI.statusText.text
		natives.UI.SET_TEXT_FONT(0)
		natives.UI.SET_TEXT_SCALE(0.55, 0.55)
		natives.UI.SET_TEXT_COLOUR(255, 255, 255, 255)
		natives.UI.SET_TEXT_WRAP(0.0, 1.0)
		natives.UI.SET_TEXT_OUTLINE(1)
		natives.UI.SET_TEXT_CENTRE(true)
		natives.UI.SET_TEXT_DROPSHADOW(1, 0, 0, 255, 255)
		natives.UI.SET_TEXT_EDGE(1, 0, 0, 0, 205)
		natives.UI._SET_TEXT_ENTRY("STRING")
		natives.UI._ADD_TEXT_COMPONENT_STRING(AnimatedActionsGUI.statusText.text)
		natives.UI._DRAW_TEXT(0.5, 0.9)
	else
		--clear the text on screen when time is up
		AnimatedActionsGUI.statusText = {["text"] = "", ["time"] = 0}
		nextDrawTime = nil
	end
end

function AnimatedActionsGUI.drawAnimatedActionsGUI()
	--this is pretty much a work around because using a while loop causes the game to freeze/crash
	--and using main:Wait(0) causes the text to flash instead of constant
	if(AnimatedActionsGUI.activeMenu ~= "" and AnimatedActionsGUI.activeMenu ~= nil) then
		local name = AnimatedActionsGUI.activeMenu

		if(AnimatedActionsGUI.menu[name] ~= nil) then
			--draw title for one frame
			local xpos = AnimatedActionsGUI.menu[name].xpos
			local ypos = AnimatedActionsGUI.menu[name].ypos
			local xscale = AnimatedActionsGUI.menu[name].xscale
			local yscale = AnimatedActionsGUI.menu[name].yscale
			
			local text = AnimatedActionsGUI.menu[name].title
			local textScale = AnimatedActionsGUI.menu[name].textScale
			local font = AnimatedActionsGUI.menu[name].font
			
			local colors = AnimatedActionsGUI.titleColor

			AnimatedActionsGUI.addButtonText(text, xpos, ypos - 0.007, textScale, font)
			natives.GRAPHICS.DRAW_RECT(xpos, ypos, xscale, yscale, colors.r, colors.g, colors.b, colors.a);

			--done drawing the title for that frame, now draw the buttons
			if(AnimatedActionsGUI.buttons[name] ~= nil) then
				for i, v in pairs(AnimatedActionsGUI.buttons[name].settings) do
					local buttonSettings = AnimatedActionsGUI.buttons[name].settings[i]
					local xpos = buttonSettings.xpos
					local ypos = buttonSettings.ypos
					local xscale = buttonSettings.xscale
					local yscale = buttonSettings.yscale
					
					local font = buttonSettings.font
					local text = buttonSettings.text
					local textScale = buttonSettings.textScale
					local colors = AnimatedActionsGUI.buttonColor

					if(buttonSettings.toggledOn == true) then
						text = buttonSettings.text .. " [ON]"
					elseif(buttonSettings.toggledOn == false) then
						text = buttonSettings.text .. "~c~ [OFF]"
					end
					
					if(i == AnimatedActionsGUI.activeButton) then
						colors = AnimatedActionsGUI.activeButtonColor
					end

					AnimatedActionsGUI.addButtonText(text, xpos, ypos, textScale, font)
					natives.GRAPHICS.DRAW_RECT(xpos, ypos, xscale, yscale, colors.r, colors.g, colors.b, colors.a)
				end
			else
				print("Menu ", name, " has no buttons!")
				AnimatedActionsGUI.setActiveMenu("")
			end
		else
			print("No menu created with the name: ", AnimatedActionsGUI.activeMenu)
			AnimatedActionsGUI.setActiveMenu("")
		end
	end
end

function AnimatedActionsGUI.addButtonText(text, xpos, ypos, textScale, font)
	if(text ~= nil and xpos ~= nil and ypos ~= nil and textScale ~= nil and font ~= nil) then
		natives.UI.SET_TEXT_FONT(font)
		natives.UI.SET_TEXT_SCALE(0.0, textScale)
		natives.UI.SET_TEXT_COLOUR(255, 255, 255, 255)
		natives.UI.SET_TEXT_CENTRE(true)
		natives.UI.SET_TEXT_OUTLINE(1)
		natives.UI.SET_TEXT_DROPSHADOW(1, 0, 0, 255, 255)
		natives.UI.SET_TEXT_EDGE(0, 0, 0, 0, 0)
		natives.UI._SET_TEXT_ENTRY("STRING")
		natives.UI._ADD_TEXT_COMPONENT_STRING(text)
		natives.UI._DRAW_TEXT(xpos, ypos - 0.0125)
	else
		print("text: ", text, ", xpos: ", xpos, ", ypos: ", ypos, ", textScale: ", textScale, ", font: ", font)
	    print("There seems to be a nil value when trying to add text to button (see above)")
	    --AnimatedActionsGUI.setActiveMenu("")
	end
end

function AnimatedActionsGUI.runButtonFunct(currButton)
	--getting the type of a table broke in one of the updates so this is a workaround
	if(type(currButton.funct) == "function" and currButton.args ~= nil and type(currButton.args) ~= "string" and type(currButton.args) ~= "number" and type(currButton.args) ~= "boolean") then
		currButton.funct(currButton.args[1], currButton.args[2], currButton.args[3], currButton.args[4], currButton.args[5], currButton.args[6], currButton.args[7], currButton.args[8], currButton.args[9], currButton.args[10], currButton.args[11], currButton.args[12], currButton.args[13], currButton.args[14], currButton.args[15], currButton.args[16], currButton.args[17], currButton.args[18], currButton.args[19], currButton.args[20])
		return true
	end

	if(type(currButton.funct) == "function" and type(currButton.args) == "string" or type(currButton.args) == "number" or type(currButton.args) == "boolean") then
		currButton.funct(currButton.args)
		return true
	end

	if(currButton.funct ~= nil and currButton.args == nil  ) then
		currButton.funct()
		return true
	end

	if(currButton.funct == nil) then
		print("Unabled to run the function")
		return false
	end
end

--this needs to be called every frame
function AnimatedActionsGUI.tick()
	--this is pretty much a work around because using a while loop causes the game to freeze/crash
	--and using main:Wait(0) causes the text to flash instead of constant
	--this is called with the main:Run() loop
	AnimatedActionsGUI.drawStatusText()
	AnimatedActionsGUI.drawAnimatedActionsGUI()

	--on menu open key press
	for i, key in pairs(AnimatedActionsGUI.navButtons.menuOpen.keys) do
		if(IsKeyDown(key)) then
			natives.AUDIO.PLAY_SOUND_FRONTEND(-1, "QUIT_WHOOSH", "HUD_MINI_GAME_SOUNDSET", false)
			if(AnimatedActionsGUI.activeMenu == AnimatedActionsGUI.navButtons.menuOpen.menu) then
				--if menu that is trying to be drawn is already drawn, go to the previous menu that is set for that menu 
				AnimatedActionsGUI.setActiveMenu(AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].prevMenu)
			else
				--if it isn't already drawn, draw as normal
			    AnimatedActionsGUI.setActiveMenu(AnimatedActionsGUI.navButtons.menuOpen.menu)
			    AnimatedActionsGUI.currentSelection = 1
			end
		end
	end

	--on menu close key press
	for i, key in pairs(AnimatedActionsGUI.navButtons.menuClose.keys) do
		if(IsKeyDown(key)) then
			natives.AUDIO.PLAY_SOUND_FRONTEND(-1, "Pin_Centred", "DLC_HEIST_BIOLAB_PREP_HACKING_SOUNDS", false)
			AnimatedActionsGUI.setActiveMenu("")
		end
	end

	--if the navUp key is pressed, move up (down numerically) in the menu
	for k, key in pairs(AnimatedActionsGUI.navButtons.navUp.keys) do
		if(IsKeyDown(key) and AnimatedActionsGUI.activeMenu ~= "") then
			AnimatedActionsGUI.activeButton = AnimatedActionsGUI.activeButton - 1
			AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].lastButton = AnimatedActionsGUI.activeButton
			natives.AUDIO.PLAY_SOUND_FRONTEND(-1, "Pin_Centred", "DLC_HEIST_BIOLAB_PREP_HACKING_SOUNDS", false)
			
			--loop selection to the last button if <= 0
			if(AnimatedActionsGUI.activeButton <= 0) then
				AnimatedActionsGUI.activeButton = AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].numButtons
				AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].lastButton = AnimatedActionsGUI.activeButton
			end
		end
	end

	--if the navDown key is pressed, move down (up numerically) in the menu
	for k, key in pairs(AnimatedActionsGUI.navButtons.navDown.keys) do
		if(IsKeyDown(key) and AnimatedActionsGUI.activeMenu ~= "") then
			AnimatedActionsGUI.activeButton = AnimatedActionsGUI.activeButton + 1
			AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].lastButton = AnimatedActionsGUI.activeButton
			natives.AUDIO.PLAY_SOUND_FRONTEND(-1, "Pin_Centred", "DLC_HEIST_BIOLAB_PREP_HACKING_SOUNDS", false)

			--loop selection to the first button if > the last button
			if(AnimatedActionsGUI.activeButton > AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].numButtons) then
				AnimatedActionsGUI.activeButton = 1
				AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].lastButton = AnimatedActionsGUI.activeButton
			end
		end
	end

	--if the select key is pressed, call the function the button was pressed on
	for k, key in pairs(AnimatedActionsGUI.navButtons.select.keys) do
		if(IsKeyDown(key) and AnimatedActionsGUI.activeMenu ~= "") then
			
			local currButton = AnimatedActionsGUI.buttons[AnimatedActionsGUI.activeMenu].settings[AnimatedActionsGUI.activeButton]
			natives.AUDIO.PLAY_SOUND_FRONTEND(-1, "Pin_Centred", "DLC_HEIST_BIOLAB_PREP_HACKING_SOUNDS", false)

			if(AnimatedActionsGUI.runButtonFunct(currButton)) then
				if(currButton.toggleable) then
					AnimatedActionsGUI.toggleActiveButtonState()
				end
			end
		end
	end

	--if the navBack key is pressed, draw the prevMenu
	for k, key in pairs(AnimatedActionsGUI.navButtons.navBack.keys) do
		if(IsKeyDown(key) and AnimatedActionsGUI.activeMenu ~= "") then
			AnimatedActionsGUI.setActiveMenu(AnimatedActionsGUI.menu[AnimatedActionsGUI.activeMenu].prevMenu)
			natives.AUDIO.PLAY_SOUND_FRONTEND(-1, "Pin_Centred", "DLC_HEIST_BIOLAB_PREP_HACKING_SOUNDS", false);
		end
	end
end

function AnimatedActionsGUI:OnError()
	--self:Kill()
	--addon.Load("RENAMEME")
end